package own.stu.jobgib.playown.juc.completeFuture;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

@Slf4j
public class CompleteFutureTest {
    private String name;

    public static void main(String[] args) {
//        ex();
//        ex2();
        baisc();
    }


    private static void ex2() {
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println(" ----- do business");
            try {
                TimeUnit.MILLISECONDS.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
//            int i = 10 / 0;
            return 0;
        }).thenApply(result -> {
            System.out.println(result);
            int i = 10 / 0;
            return 2;
        }).exceptionally(err -> {
            log.error("Exception {}", err);
            return null;
        });

        future.join();
    }

    private static void ex() {
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println(" ----- do business");
            try {
                TimeUnit.MILLISECONDS.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int i = 10 / 0;
            return 0;
        }).exceptionally(err -> {
            log.error("Exception {}", err);
            return null;
        });

        future.join();
    }

    private static void baisc() {
        CompletableFuture.runAsync(() -> {
            System.out.println("1111111");
        });
        CompletableFuture.supplyAsync(() -> {
            System.out.println("xxxx");
            return 1001;
        }).thenAccept(res -> {
            System.out.println(res + " yyyyyyyy");
        });
        System.out.println("-----");
    }
}
